<?php

namespace App\Repositories\Category;

use App\Criteria\Category\GetByCatIdCriteria;
use Illuminate\Support\Facades\DB;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Repositories\Category\CategoryRepository;
use App\Models\Category\Category;
use App\Validators\Category\CategoryValidator;

/**
 * Class CategoryRepositoryEloquent.
 *
 * @package namespace App\Repositories\Category;
 */
class CategoryRepositoryEloquent extends BaseRepository implements CategoryRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Category::class;
    }

    /**
    * Specify Validator class name
    *
    * @return mixed
    */
    public function validator()
    {

        return CategoryValidator::class;
    }


    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    /**
     * 根据分类id获取分类名称
     * @author Min
     * @param $cat_id
     * @return mixed
     */
    public function getCatNameByCatId($cat_id){
        $where = [
            'cat_id' => $cat_id
        ];

        $res = $this->findWhere($where, ['cat_name']);
        $res = $res->first();
        return $res['cat_name'];
    }

    /**
     * 根据分类id获取分类列表
     * @author Min
     * @param $cat_id
     * @return mixed
     */
    public function getCategoryListByCatId($cat_id){
        $where = [
            'is_show' => 1,
            'cat_id' => $cat_id
        ];
        $field = ['cat_id','cat_name','parent_id'];

        return $this->findWhere($where, $field)->toArray();
    }

    /**
     * 根据多个分类id获取分类列表
     * @author Min
     * @param $cat_id_list
     * @return mixed
     * @throws \Prettus\Repository\Exceptions\RepositoryException
     */
    public function getCategoryListInCatId($cat_id_list){
        $where = [
            'is_show' => 1,
        ];
        $field = ['cat_id','cat_name','parent_id'];

        return $this->makeModel()
                    ->where($where)
                    ->whereIn('cat_id', $cat_id_list)
                    ->get($field)
                    ->toArray();
    }

    public function getAllCategory(){
        return $this->all()->toArray();
    }
}
